#include "Random.h"

double Random::randomUniform(double min, double max)
{
	double r;
	r=(double)rand()/RAND_MAX;
	r=min+r*(max-min);
	return r;
}

double Random::randomNormal(double mean, double std)
{
	double x1, x2, w, y1;
	do
		{
			x1 = 2.0 * randomUniform(0,1) - 1.0;
			x2 = 2.0 * randomUniform(0,1) - 1.0;
			w = x1 * x1 + x2 * x2;
		}
	while ( w >= 1.0);
	w = sqrt( (-2.0 * log( w ) ) / w );
	y1 = x1 * w;
	y1 = (y1 * std) + mean;
	return y1;
}

void Random::randomSphere(double radius=80)
{
	double l=0;
	for (int j=0; j<3; j++)
		{
			Tabela[j] = randomNormal(0,1);
			l += Tabela[j]*Tabela[j];
		}
	l = sqrt(l);
	double r = randomUniform(0,1);
	r = pow( r, 1/3);
	for (int j=0; j<3; j++)
		{
			Tabela[j] = radius*(r * Tabela[j] / l);
		}
}
